/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.QuyDinhDoanTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class QuyDinhDoanTau_DAO {
    
    public static String Get_MaQD() throws SQLException{
        String MaQD = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call ADD_ID(?)}");
        statement.setString(1, "QD");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaQD = Rs.getString(1);
        }
        return MaQD;
    }
    
    public static ArrayList<QuyDinhDoanTau_DTO> GetALL()
    {
        ArrayList<QuyDinhDoanTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from QuyDinhDoanTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                QuyDinhDoanTau_DTO QuyDinh = new QuyDinhDoanTau_DTO();
                QuyDinh.setID(rs.getInt("ID"));
                QuyDinh.setSoVeMax(rs.getInt("SoVeMax"));
                QuyDinh.setHanThanhToan(rs.getInt("HanDatVe"));
                QuyDinh.setHanThanhToan(rs.getInt("HanThanhToan"));
                QuyDinh.setMaDT(rs.getString("MaDT"));
                ds.add(QuyDinh);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(QuyDinhDoanTau_DTO QuyDinh)
    {
        boolean flag = false;
        try {
            String sql = "Insert QuyDinhDoanTau Values('"+QuyDinh.getID()+"','"+QuyDinh.getSoVeMax()+"','"+QuyDinh.getHanDatVe()+"',"+QuyDinh.getHanThanhToan()+"','"+QuyDinh.getMaDT()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(QuyDinhDoanTau_DTO QuyDinh)
    {
        boolean flag = false;
        try {
            String sql = "Update QuyDinhDoanTau Set SoVeMax = "+ QuyDinh.getSoVeMax() +", HanDatVe = "+QuyDinh.getHanDatVe()+", HanThanhToan = "+QuyDinh.getHanThanhToan()+", MaDT ='"+ QuyDinh.getMaDT()+ "' where ID = "+ QuyDinh.getID();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from QuyDinhDoanTau where ID = '"+maID+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
